package com.deparse.mydialog.dialog;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.deparse.mydialog.R;
import com.deparse.mydialog.common.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/18.
 */
public class AndroidDialog extends BaseActivity {
    private ListView listView;
    private AdapterList adapterList;
    private List<String> listData = new ArrayList<>();
    private LayoutInflater layoutInflater;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_dialog);

        layoutInflater = LayoutInflater.from(this);
        listView = (ListView) findViewById(R.id.listView);

        listData.add("NormalDialog");
        listData.add("ViewDialog");
        listData.add("ListDialog");
        listData.add("SingleListDialog");
        listData.add("MultiselectListDialog");
        listData.add("NoTitleViewDialog");
        listData.add("MyDialog");

        adapterList = new AdapterList();
        listView.setAdapter(adapterList);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        showNormalDialog();
                        break;
                    case 1:
                        showViewDialog();
                        break;
                    case 2:
                        showListDialog();
                        break;
                    case 3:
                        showSingleListDialog();
                        break;
                    case 4:
                        showMultiselectListDialog();
                        break;
                    case 5:
                        showNoTitleViewDialog();
                        break;
                    case 6:
                        showMyDialog();
                        break;
                }
            }
        });
    }

    private void showMyDialog() {
        LoadingDialog dialog = new LoadingDialog(this);
        dialog.show();
    }

    private void showNormalDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认退出吗？");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    private void showViewDialog() {
        new AlertDialog.Builder(this).setTitle("请输入").setIcon(
                android.R.drawable.ic_dialog_info).setView(
                new EditText(this)).setPositiveButton("确定", null)
                .setNegativeButton("取消", null).show();
    }

    private void showListDialog() {
        new AlertDialog.Builder(this).setTitle("列表框").setItems(
                new String[]{"Item1", "Item2"}, null).setNegativeButton(
                "确定", null).show();
    }

    private void showSingleListDialog() {
        new AlertDialog.Builder(this).setTitle("单选框").setIcon(
                android.R.drawable.ic_dialog_info).setSingleChoiceItems(
                new String[]{"Item1", "Item2"}, 0,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).setNegativeButton("取消", null).show();
    }

    private void showMultiselectListDialog() {
        new AlertDialog.Builder(this).setTitle("复选框").setMultiChoiceItems(
                new String[]{"Item1", "Item2"}, null, null)
                .setPositiveButton("确定", null)
                .setNegativeButton("取消", null).show();
    }

    private void showNoTitleViewDialog() {
        new AlertDialog.Builder(this).setView(
                new EditText(this)).show();
    }


    private class AdapterList extends BaseAdapter {

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = layoutInflater.inflate(R.layout.item_main_list, null);
            TextView activity_name = (TextView) convertView.findViewById(R.id.activity_name);
            activity_name.setText(listData.get(position));
            return convertView;
        }
    }
}
